package com.tb.zookeeper;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.Scanner;

/**
 * 作者：tb
 * 描述：todo
 * 日期：Created in 2022/4/7 11:44
 *
 * @Version 1.0
 */
public class CilentListenerTest {
    public static void main(String[] args) {
        try{
            System.out.println("启动了客户端的监听");
            ZkClient zkClient = new ZkClient("127.0.0.1:2184",5000);
            zkClient.subscribeDataChanges("/myage", new IZkDataListener() {
                public void handleDataChange(String dataPath,Object data) throws Exception {
                    System.out.println("目标节点"+dataPath+"，修改了"+data.toString());
                }

                public void handleDataDeleted(String dataPath) throws Exception {
                    System.out.println("目标节点"+dataPath+"，被删除了");
                }
            });
            //必须通过线程阻塞的方式让监听器一直处于运行状态
            System.out.println("输入任何内容结束");
            new Scanner(System.in).next();
            System.out.println("程序结束");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
